//
//  SideWayTableController.m
//  CareLink
//
//  Created by 赵玉忠 on 16/8/18.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLDrawViewController.h"
#import "CLHomeViewController.h"
#import "SideWayNavController.h"
#import "SideWayTableController.h"
#import "UMSocial.h"
#import "CLAEViewController.h"
#import "CLClickBtnOtherViewController.h"
#import "CLPersonInfoControllerT.h"
@interface SideWayTableController ()
/**
 *  死数据
 */
@property (nonatomic, strong) NSArray* dataArray;
/**
 *  子控制器数据
 */
@property (nonatomic, strong) NSArray* controllerArray;
/**
 *  两个给共享提供的全局属性
 */
@property (nonatomic, strong) UIView* bigView;
@property (nonatomic, strong) UIView* bottomView;
@end

@implementation SideWayTableController

/**
 *  判断是否登录的全局变量
 */
//static BOOL isLoad;

-(void)viewWillAppear:(BOOL)animated{
    NSString *str= [[NSUserDefaults standardUserDefaults] stringForKey:KisLoad];
    NSLog(@"%@",str);
    self.isLoad = [str intValue];
    
    [self setTableHeaderView];
    [self.tableView reloadData];
}
- (void)viewDidLoad
{

//    self.isLoad = NO;
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadData];
    [self setup];
}

- (void)setup
{

    UITableView* tableView = [[UITableView alloc] initWithFrame:self.tableView.bounds style:UITableViewStyleGrouped];
    self.tableView = tableView;
    self.tableView.rowHeight = 60;

    //取消分割线
    //        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
    //设置头部视图
    [self setTableHeaderView];
}

#pragma mark - 设置头部视图
- (void)setTableHeaderView
{
    UIView* headerView;
    if (self.isLoad == NO) {
        headerView = [self noIsLoadHeaderView];
    }
    else {
        headerView = [self IsLoadHeaderView];
    }
    //设置头视图
    self.tableView.tableHeaderView = headerView;
    return;
}
#pragma mark - 未登录时头部视图
- (UIView*)noIsLoadHeaderView
{
    UIView* headerView = [[UIView alloc] init];
    //添加头像
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name"]];
    [headerView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
        //        make.left.offset(13);
        make.top.offset(25);
        //        make.bottom.equalTo(headerView.mas_bottom).offset(-5);
        make.centerX.equalTo(headerView.mas_centerX);
        make.width.height.offset(40);
    }];
#pragma mark - 登录，删除按钮
    //登录按钮
    UIButton* loadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [loadButton addTarget:self action:@selector(load) forControlEvents:UIControlEventTouchUpInside];
    loadButton.layer.borderWidth = 1;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef borderColorRef = CGColorCreate(colorSpace, (CGFloat[]){ 1, 1, 1, 1 });
    [loadButton.layer setBorderColor:borderColorRef];

    [loadButton setTitle:@"  登录  " forState:UIControlStateNormal];
    [loadButton setFont:[UIFont systemFontOfSize:20]];
    [loadButton setTintColor:[UIColor whiteColor]];
    [loadButton sizeToFit];
    //    loadButton.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:loadButton];
    [loadButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.right.equalTo(imageView.mas_centerX).offset(-10);
    }];

    //注册按钮
    UIButton* registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [registerButton addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    registerButton.layer.borderWidth = 1;
    CGColorSpaceRef colorSpace1 = CGColorSpaceCreateDeviceRGB();
    CGColorRef borderColorRef1 = CGColorCreate(colorSpace1, (CGFloat[]){ 1, 1, 1, 1 });
    [registerButton.layer setBorderColor:borderColorRef1];

    [registerButton setTitle:@"  注册  " forState:UIControlStateNormal];
    [registerButton setFont:[UIFont systemFontOfSize:20]];
    [registerButton setTintColor:[UIColor whiteColor]];
    [registerButton sizeToFit];
    //    loadButton.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.left.equalTo(imageView.mas_centerX).offset(10);
    }];

    headerView.backgroundColor = [UIColor colorWithHex:0x20c6c6];
    //获取头视图的宽
    CGFloat HeaderViewWidth = self.tableView.bounds.size.width;
    //计算头视图正确的高度
    CGFloat HeaderViewHeight = 120;
    headerView.frame = CGRectMake(0, 0, HeaderViewWidth, HeaderViewHeight);
    return headerView;
}
#pragma mark - 跳转到登录界面
- (void)load
{
    NSLog(@"跳转到登录界面");
//    isLoad = YES;
    

        CLAEViewController *login = [[CLAEViewController alloc]init];
        login.navigationItem.title = @"用户登录";
    
    [self pushTo:login];
    
    
}
#pragma mark - 跳转方法
-(void)pushTo:(UIViewController *)sender{
    CLDrawViewController* drawViewController = (CLDrawViewController*)self.parentViewController;
    
    [drawViewController closeDrawerAnimated:YES completion:^(BOOL finished){
    }];
    UINavigationController* nav = (UINavigationController*)drawViewController.centerViewController;
    [nav pushViewController:sender animated:NO];
}
#pragma mark - 跳转到注册界面
- (void)regist
{
    NSLog(@"跳转到注册界面");
    CLClickBtnOtherViewController *clc = [[CLClickBtnOtherViewController alloc]init];
    clc.navigationItem.title = @"注册";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    clc.navigationItem.backBarButtonItem = backItem;

    [self pushTo:clc];
}

#pragma mark - 登录时头部视图
- (UIView*)IsLoadHeaderView
{
    UIView* headerView = [[UIView alloc] init];
    //添加头像
//    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name"]];
    UIImageView *imageView = [[UIImageView alloc]init];
    
    //获取头像视图
    NSString *path = [[NSUserDefaults standardUserDefaults]objectForKey:KheadImage];
    
//    imageView.layer.cornerRadius = imageView.frame.size.width * 0.5;
        imageView.layer.cornerRadius = 30;
    imageView.layer.masksToBounds = YES;

//    UIImage *image = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    if (image == nil) {
        imageView.image = [UIImage imageNamed:@"name"];
    }else{
        imageView.image = image;
    }
    
    [headerView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.offset(13);
        make.centerY.equalTo(headerView.mas_centerY);
        make.width.height.offset(60);
    }];
    
    //联系方式
    NSInteger fontText = 15;
    UILabel* labelContact = [[UILabel alloc] init];
    
    //获取电话号码
    NSString *phoneNumber = [[NSUserDefaults standardUserDefaults] stringForKey:KUserName];
    if (phoneNumber == nil) {
        labelContact.text = @"18301166921";
    }else{
        labelContact.text = phoneNumber;
    }

    NSLog(@"%@",phoneNumber);
    labelContact.font = [UIFont systemFontOfSize:fontText];
    if (self.contact != nil) {
        labelContact.text = self.contact;
    }
    labelContact.textColor = [UIColor whiteColor];
    [headerView addSubview:labelContact];
    [labelContact mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.top.equalTo(imageView).offset(5);
    }];
    [labelContact sizeToFit];

    //获取昵称
    NSString *smallName = [[NSUserDefaults standardUserDefaults] stringForKey:KsmallName];

    //基本信息label
    UILabel* labelText = [[UILabel alloc] init];
    if (smallName == nil) {
        labelText.text = @"西门吹雪";
    }else{
        labelText.text = smallName;
    }
    labelText.font = [UIFont systemFontOfSize:fontText];
    labelText.textColor = [UIColor whiteColor];
    [headerView addSubview:labelText];
    [labelText mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.bottom.equalTo(imageView.mas_bottom).offset(-5);
    }];
    [labelText sizeToFit];

    imageView.layer.cornerRadius = 40 * 0.5;
    headerView.backgroundColor = [UIColor colorWithHex:0x20c6c6];
    //获取头视图的宽
    CGFloat HeaderViewWidth = self.tableView.bounds.size.width;
    //计算头视图正确的高度
    CGFloat HeaderViewHeight = 120;
    headerView.frame = CGRectMake(0, 0, HeaderViewWidth, HeaderViewHeight);

    //添加轻触手势
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    //2.添加
    [headerView addGestureRecognizer:tap];

    return headerView;
}
#pragma mark - 跳转到个人休息修改页面
- (void)tap:(UITapGestureRecognizer*)sender
{
    NSLog(@"跳转到个人修改页面");
    CLPersonInfoControllerT *cl = [[CLPersonInfoControllerT alloc]init];
    //    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"personInfo" bundle:nil];
    //    UIViewController *vc = sb.instantiateInitialViewController;
    [self pushTo:cl];
    
}

#pragma mark - 加载数据
- (void)loadData
{
    //1.获取全路径
    NSString* path = [[NSBundle mainBundle] pathForResource:@"SideWayPlist.plist" ofType:nil];
    //2.读取数据
    self.dataArray = [NSArray arrayWithContentsOfFile:path];
}
#pragma mark - 监听cell 的点击
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    //点击时立即取消背景
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 1 && indexPath.row == 2) {

        [self click];
        return;
    }
    //解析文件
    //1.获取全路径
    NSString* path = [[NSBundle mainBundle] pathForResource:@"SideWayChild.plist" ofType:nil];
    //2.读取数据
    self.controllerArray = [NSArray arrayWithContentsOfFile:path];

    NSArray* array = self.controllerArray[indexPath.section];
    NSString* str = array[indexPath.row];
    Class Clz = NSClassFromString(str);

    //创建控制器跳转
    UIViewController* vc = [[Clz alloc] init];
    CLDrawViewController* drawViewController = (CLDrawViewController*)self.parentViewController;
    [drawViewController closeDrawerAnimated:YES completion:^(BOOL finished){
    }];
    UINavigationController* nav = (UINavigationController*)drawViewController.centerViewController;
    [nav pushViewController:vc animated:NO];

    
    //覆盖导航栏的view
    UIView* view = [[UIView alloc] init];
    UIImageView* imageView = [[UIImageView alloc] init];
    [view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"nav"];
    [imageView sizeToFit];
    [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.bottom.left.right.offset(0);
    }];
    vc.navigationItem.titleView = imageView;

    UILabel* label = [[UILabel alloc] init];
    [imageView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(imageView);
        make.centerY.equalTo(imageView);
    }];

    //
    NSArray* array1 = self.dataArray[indexPath.section];
    label.text = array1[indexPath.row];
}
#pragma mark - 点击分享
- (void)click
{

    CLDrawViewController* drawViewController = (CLDrawViewController*)self.parentViewController;
    [UMSocialSnsService presentSnsIconSheetView:drawViewController
                                         appKey:@"57b80409e0f55ad0e1002247"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"Icon"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToRenren,UMShareToDouban,
                                                 nil]
                                       delegate:nil];
}

//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    if (self.isLoad == YES) {
        return self.dataArray.count;
    }
    else {
        return 0;
    }
}
//行数
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray* array = self.dataArray[section];
    return array.count;
}
//cell
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //赋值
    NSArray* smallArray = self.dataArray[indexPath.section];
    cell.textLabel.text = smallArray[indexPath.row];
    return cell;
}

@end


